package com.mathassistant.client.view;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.Label;
import com.mathassistant.client.presenter.ExerciseManagementPresenter.Display;

public class ExerciseManagementView extends Composite implements Display{

	private static ExerciseManagementViewUiBinder uiBinder = GWT
			.create(ExerciseManagementViewUiBinder.class);
	@UiField FlexTable listExercise;
	@UiField Button backButton;
	@UiField Button nextButton;
	@UiField Label message;

	interface ExerciseManagementViewUiBinder extends
			UiBinder<Widget, ExerciseManagementView> {
	}

	public ExerciseManagementView() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public HasClickHandlers getNextButton() {
		// TODO Auto-generated method stub
		return nextButton;
	}

	@Override
	public HasClickHandlers getBackButton() {
		// TODO Auto-generated method stub
		return backButton;
	}

	@Override
	public HasClickHandlers getListExerciseTable() {
		// TODO Auto-generated method stub
		return listExercise;
	}

	@Override
	public int getEditButton(ClickEvent event) {
		// TODO Auto-generated method stub
		int selectedRow = -1;
		HTMLTable.Cell cell = this.listExercise.getCellForEvent(event);

		if (cell != null) {
			// Suppress clicks if not on the property button
			if (cell.getCellIndex() == 2) {
				selectedRow = cell.getRowIndex();
			}
		}

		return selectedRow;
	}

	@Override
	public int getDeleteButton(ClickEvent event) {
		// TODO Auto-generated method stub
		int selectedRow = -1;
		HTMLTable.Cell cell = this.listExercise.getCellForEvent(event);

		if (cell != null) {
			// Suppress clicks if not on the property button
			if (cell.getCellIndex() == 1) {
				selectedRow = cell.getRowIndex();
			}
		}

		return selectedRow;
	}

	@Override
	public void setBackNextButtonView(int indexCurrent, int total) {
		// TODO Auto-generated method stub
		if(indexCurrent < 10)
			backButton.setEnabled(false);
		else
			backButton.setEnabled(true);
		if(total > indexCurrent + 10){
			nextButton.setEnabled(true);
		}
		else{
			nextButton.setEnabled(false);
		}
	}

	@Override
	public void toEmptyExercise() {
		// TODO Auto-generated method stub
		listExercise.clear();
		message.setVisible(true);
	}

	@Override
	public void notEmpty() {
		// TODO Auto-generated method stub
		message.setVisible(false);
	}

	@Override
	public void setData(List<String> data, int indexBegin) {
		// TODO Auto-generated method stub
		listExercise.clear();
		if(data == null || data.size() == 0){
			return;
		}
		for(int i = 0; i < data.size(); i++){
			
			Button num = new Button(String.valueOf(indexBegin+i));
			num.addStyleName("button-number");
			num.setEnabled(false);
			listExercise.setWidget(i, 0, num);
			
			Button delete = new Button("Xóa");
			listExercise.setWidget(i, 1, delete);
			delete.addStyleName("button-xoa-sua");
			
			Button edit = new Button("Sửa");
			listExercise.setWidget(i, 2, edit);
			edit.addStyleName("button-xoa-sua");
			
			listExercise.setHTML(i, 3, data.get(i));
			listExercise.getCellFormatter().addStyleName(i, 3, "itemquestion");
			if(i % 2 == 0){
				for(int j = 0; j < 4; j++)
					listExercise.getCellFormatter().addStyleName(i, j, "itemEven");
			} else {
				for(int j = 0; j < 4; j++)
					listExercise.getCellFormatter().addStyleName(i, j, "itemUnEven");
			}
		}
	}

	@Override
	public Widget asWidget() {
		// TODO Auto-generated method stub
		return this;
	}
}
